<?php

class MatchAPIController extends \BaseController {

	/**
	 * Controller constructor
	 */
	public function __construct() {
	}
	
	/**
	 * Display a listing of matches.
	 *
	 * @return Response
	 */
	public function index()
	{
		$matches = Match::all();
		foreach($matches as $value){
			$value->admin;
			$value->field;
			$value->type;
		}
		return Response::json($matches);
	}

	/**
	 * Store a newly created match in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function store()
	{
		$newMatch = Input::json();
	
		//create match
		$match = new Match();
		$match->date = $newMatch->get('date');
		$match->comments = $newMatch->get('comments');
		$match->fieldId = $newMatch->get('fieldId');
		$match->matchTypeId = $newMatch->get('matchTypeId');
		$match->admin_userId = $newMatch->get('admin_userId');
		$match->cancelled = $newMatch->get('cancelled');
		$match->token = trim($this->generate_GUID(), '{}');
		$match->save();
	
		$versus = $match->type()->first()->versus;
		
		//create teams
		$teamIds = array();
		
		$teamA = new Team();
		$teamA->name = "A";
		$teamA->shirtColor = "#fff";
		$teamA->save();
		$teamA->users()->sync([$match->admin_userId]);
		$teamIds[] = $teamA->id;
		
		if(!$versus){
			$teamB = new Team();
			$teamB->name = "B";
			$teamB->shirtColor = "#000";
			$teamB->save();
			$teamIds[] = $teamB->id;
		}
		$match->teams()->sync($teamIds);
		
		
		
		return Response::json($match);
	}


	/**
	 * Display the specified match.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$match = Match::find($id);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			//players confirmed
			$guestsConfirmedAmount = $match->guests()->wherePivot('confirmed', 1)->count() + 1;
			$match->guestsConfirmedAmount = $guestsConfirmedAmount;
			
			$match->admin;
			$match->field;
			$match->type;
			return Response::json($match);
		}
	}
	
	/**
	 * Display the specified match by token.
	 *
	 * @param  string  $token
	 * @return Response
	 */
	public function getByToken($token)
	{
		$token = urldecode ($token);
		$match = Match::where('token', '=', $token)->firstOrFail();
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			//players confirmed
			$guestsConfirmedAmount = $match->guests()->wherePivot('confirmed', 1)->count() + 1;
			$match->guestsConfirmedAmount = $guestsConfirmedAmount;
			
			$match->guests;
			$match->admin;
			$match->field;
			$match->type;
			return Response::json($match);
		}
	}


	/**
	 * Update the specified match in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function update($id)
	{
		$updateMatch = Input::json();
		
		$match = Match::find($id);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		}
		$match->date = $updateMatch->get('date');
		$match->comments = $updateMatch->get('comments');
		$match->fieldId = $updateMatch->get('fieldId');
		$match->matchTypeId = $updateMatch->get('matchTypeId');
		$match->admin_userId = $updateMatch->get('admin_userId');
		$match->cancelled = $updateMatch->get('cancelled');
		$match->save();
	
		return Response::json($match);
	}


	/**
	 * Remove the specified match from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroy($id)
	{
		$match = Match::find($id);
	
		if(is_null($match))
		{
			return Response::json('Match not found', 404);
		}
		$deletedMatch = $match;
		$match->delete();
		return Response::json($deletedMatch);
	}
	
	/**
	 * Display the admin user of the specified match in JSON format.
	 *
	 * @param $matchId
	 * @return JSON Response
	 */
	public function getAdminUser($matchId)
	{
		$match = Match::find($matchId);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			return Response::json($match->admin);
		}
	}
	
	/**
	 * Display the teams and its users of the specified match in JSON format.
	 *
	 * @param $matchId
	 * @return JSON Response
	 */
	public function getTeams($matchId)
	{
		$match = Match::find($matchId);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			foreach($match->teams as $value){
				$value->users;
			}
			return Response::json($match->teams);
		}
	}
	
	/**
	 * Display the substitute users of the specified match in JSON format.
	 *
	 * @param $matchId
	 * @return JSON Response
	 */
	public function getSubs($matchId)
	{
		$match = Match::find($matchId);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			return Response::json($match->substitutes);
		}
	}
	
	/**
	 * Display the guest users of the specified match in JSON format.
	 *
	 * @param $matchId
	 * @return JSON Response
	 */
	public function getGuests($matchId)
	{
		$match = Match::find($matchId);
		if(is_null($match)){
			return Response::json('Match not found', 404);
		} else {
			return Response::json($match->guests);
		}
	}
	
	/**
	 * Update substitute specified users into specified team by JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function updateSubs()
	{
		$data = Input::json();
	
		// store relation to substitute Users
		$matchId = $data->get('matchId');
		$match = Match::find($matchId);
		$userFBIds = $data->get('userIds');
		if(is_array($userFBIds))
		{
			$userIds = array();
			foreach($userFBIds as $userFBId){
				$user = User::where('fbId', '=', $userFBId)->first();
				if($user != null)
					$userIds[] = $user->id;
				else
					$userIds[] = $userFBId;
			}
			$match->substitutes()->sync($userIds);
		}
	
		return Response::json($match->substitutes);
	}
	
	/**
	 * Update guest specified users into specified team by JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function updateGuests()
	{
		$data = Input::json();
	
		// store relation to substitute Users
		$matchId = $data->get('matchId');
		$match = Match::find($matchId);
		$users = $data->get('users');
		if(is_array($users))
		{
			$users_aux = array();
			foreach($users as $user){
				$userObj = User::where('fbId', '=', $user['id'])->first();
				if($userObj != null)
					$users_aux[$userObj->id] = array("confirmed" => $user['confirmed']);
				else
					$users_aux[$user['id']] = array("confirmed" => $user['confirmed']);
			}
			$match->guests()->sync($users_aux);
		}
	
		return Response::json($match->guests);
	}
}
